﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using S7.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices.ComTypes;

namespace AgvDemo
{


    public class ChargeParaClass
    {
        public ChargeParaClass()
        {

            AgvNum = 0;
            CurrentStation = 0;
            CurrentStationNoZero = 0;
            TargetStation = 0;
            AgvState = 0;

            AgvSpeed = 0;
            AgvVoltage = 0;
            AgvDirection = 0;
            AgvCmd = 0;

            AgvDirCmd = 0;
            AgvRoadCmd = 0;
            AgvError = 0;

            BoxGo = false;
        }

        public int AgvNum { get; set; }
        public int CurrentStation { get; set; }
        public int CurrentStationNoZero { get; set; }
        public int TargetStation { get; set; }
        public int AgvState { get; set; }

        public int AgvSpeed { get; set; }
        public int AgvVoltage { get; set; }
        public int AgvDirection { get; set; }
        public int AgvCmd { get; set; }

        public int AgvDirCmd { get; set; }
        public int AgvRoadCmd { get; set; }
        public int AgvError { get; set; }


        public bool BoxGo { get; set; }

    }


    public class ChargeParaJiGouClass
    {
        public ChargeParaJiGouClass()
        {
            AgvAllow = false;
            AgvLeave = false;
            PlcAllow = false;
        }

        public bool AgvAllow { get; set; }
        public bool AgvLeave { get; set; }
        public bool PlcAllow { get; set; }

    }



    public class PlcManagerClass
    {
       
        //DataBaseClass dataBase;
        //BasicFunctionClass basicFunc;
        //MonitorShowClass agvShow;


       public string ip = "";
       public int agvId = 0;

        public Plc S7_9;


        public int count9 = 0;
        public bool b9 = true;
        public Thread trafficTh9;

        public int DiaoRecord = 0;
        public bool DiaoBool = false;


        //public Plc S7Smart200;

        public static object lockPlc = new object();//防止下位机数据处理异常

        public ChargeParaClass chargeState9 = new ChargeParaClass();
       
        AgvFunctionClass agvF;
        public Ping pingAgv = new Ping();
        public bool isConnectSure = false;

        public Thread th1;
        public string monitorError = string.Empty;
        //AgvFunctionClass agvF;

        public PlcManagerClass(int agvNum,string ipT,AgvFunctionClass afc)//
        {
            ip = ipT;
            agvId = agvNum;
            agvF = afc;

            
            //basicFunc = new BasicFunctionClass();
            //agvShow = new MonitorShowClass();
          

            S7_9 = new Plc(CpuType.S7200Smart, ip, 0, 1);

            PlcThread();

        }
    
        public void PlcThread()
        {
            th1 = new Thread(new ThreadStart(ChargeThreadMethod9));
            th1.Start();
            //th1.IsBackground = true;
            ///th1.Priority = ThreadPriority.Highest;

            //MainManagerClass.thList.Add(th1);

           // Thread th2 = new Thread(new ThreadStart(ChargeThreadMethod10));
           // th2.IsBackground = true;
           //// th2.Priority = ThreadPriority.Highest;
           // th2.Start();

        }


        private void ChargeThreadMethod10()
        {
            while (true)
            {
                try
                {
                    if (th1.ThreadState == ThreadState.Unstarted || th1.ThreadState == ThreadState.Stopped)
                    {
                        th1.Start();


                    }

                    Thread.Sleep(1000);
                }
                catch
                { }
            }
         
        }

        public void ChargeThreadMethod9()
        {
            //while (true)
            {

                if (count9 > 100000)
                {
                    count9 = 0;
                }
                else
                {
                    count9++;

                }

                try
                {
                    if (pingAgv.Send(ip).Status == IPStatus.Success)
                    {
                        monitorError = "PINGOK";


                        if (isConnectSure)
                        {                      
                            try
                            {
                                var agvNumTemp = S7_9.Read(DataType.DataBlock, 1, 120, VarType.Byte, 10);
                                byte[] AgvData = (byte[])agvNumTemp;
                                chargeState9.AgvNum = AgvData[0];

                                if (chargeState9.AgvNum == agvId)
                                {
                                    chargeState9.CurrentStation = AgvData[3];

                                    if (AgvData[3] != 0 && AgvData[3] != 101 && AgvData[3] != 111 && AgvData[3] != 121 && AgvData[3] != 131 && AgvData[3] != 91 && AgvData[3] != 92 
                                        && AgvData[3] != 220 && AgvData[3] != 221 && AgvData[3] != 222 && AgvData[3] != 223 && AgvData[3] != 224
                                        && AgvData[3] != 225)
                                    {
                                        chargeState9.CurrentStationNoZero = AgvData[3];
                                    }
                                    else
                                    {
                                        chargeState9.CurrentStation = chargeState9.CurrentStationNoZero;
                                    }

                                    chargeState9.TargetStation = AgvData[1];
                                    chargeState9.AgvError = AgvData[4];
                                    chargeState9.AgvState = AgvData[6];


                                    if (chargeState9.CurrentStation != 0 && chargeState9.CurrentStation != 101 && chargeState9.CurrentStation != 111 && chargeState9.CurrentStation != 121 && chargeState9.CurrentStation != 131 && chargeState9.CurrentStation != 91 && chargeState9.CurrentStation != 92 
                                        && chargeState9.CurrentStation != 220 && chargeState9.CurrentStation != 221 && chargeState9.CurrentStation != 222 && chargeState9.CurrentStation != 223 && chargeState9.CurrentStation != 224
                                        && chargeState9.CurrentStation != 225)
                                    {

                                        MainManagerClass.agvDispatch.AgvDispatch(this);

                                        //if (chargeState9.TargetStation == 3)//仪表
                                        //{
                                        //    MainManagerClass.agvDispatch.LampDispatch(this);
                                        //    MainManagerClass.trafficManager.TrafficHandle(this);
                                            
                                        //}
                                        //else
                                        //{                                        
                                        MainManagerClass.agvDispatch.BoxDispatch(this);
                                        //    MainManagerClass.trafficManager.TrafficHandleDoor(this);
                                        //}


                                        //MainManagerClass.agvShow.AgvMonitorShow(chargeState9, agvF);
                                        MainManagerClass.basicFunc.StationRecord(chargeState9);                                       
                                    }


                                    //basicFunc.ErrorMonitor(chargeState9);

                                }
                            }
                            catch (Exception ex)
                            {
                                monitorError = ex.ToString();
                              

                                //try
                                //{
                                //    S7_9.Close();
                                //}
                                //catch
                                //{ isConnectSure = false; }
                            }

                        }
                        else
                        {
                            try
                            { 
                                S7_9.Open();
                                isConnectSure = true;
                            }
                            catch (Exception ex)
                            {
                                monitorError = ex.ToString();

                                isConnectSure = false;

                                try
                                {
                                    S7_9.Close();
                                }
                                catch
                                { isConnectSure = false; }


                                //try
                                //{
                                //    S7_9.Close();
                                //}
                                //catch
                                //{ }


                                //try
                                //{
                                //    isConnectSure = false;
                                //    S7_9 = new Plc(CpuType.S7200Smart, ip, 0, 1);

                                //}
                                //catch
                                //{ }
                            }
                           
                        }
                    }
                    else
                    {
                        monitorError = "PINGNG";

                        isConnectSure = false;

                        try
                        {
                            S7_9.Close();
                        }
                        catch
                        { isConnectSure = false; }

                        //try
                        //{
                        //    S7_9.Close();
                        //}
                        //catch
                        //{  }


                        //try
                        //{
                        //    isConnectSure = false;
                        //    S7_9 = new Plc(CpuType.S7200Smart, ip, 0, 1);
                            
                        //}
                        //catch
                        //{ }

                    
                    }
                       
                  
                }
                catch 
                {
                    isConnectSure = false;

                    try
                    {
                        S7_9.Close();
                    }
                    catch
                    { isConnectSure = false; }

                    //try
                    //{
                    //    S7_9.Close();
                    //}
                    //catch
                    //{ }


                    //try
                    //{
                    //    isConnectSure = false;
                    //    S7_9 = new Plc(CpuType.S7200Smart, ip, 0, 1);

                    //}
                    //catch
                    //{ }
                }

                Thread.Sleep(500);

                //th1.Suspend();
            }
        }

     

        public void LockAgv()
        {
            try
            {
                if (isConnectSure)
                {
                    byte[] b = { 0x01 };


                    S7_9.WriteBytes(DataType.DataBlock, 1, 154, b);
                    //lock (MainForm.lockCommand)
                    //{
                    //    MainForm.mainForm.comRec.AppendData(chargeState9.AgvNum, "锁车");

                    //}
                    //lock (AgvFunctionClass.commandList)
                    {
                        AgvCommandClass acc = new AgvCommandClass();
                        acc.AgvNum = chargeState9.AgvNum;
                        acc.CurrentStation = chargeState9.CurrentStation;
                        acc.CommandName = "锁车";
                        acc.CurrentTime = DateTime.Now.ToString();
                        AgvFunctionClass.commandList.Add(acc);
                    }
                }
             
            }
            catch
            { }
        }



        public void StartAgv()
        {
            try
            {
                if (isConnectSure)
                {
                    byte[] b = { 0x01 };


                    S7_9.WriteBytes(DataType.DataBlock, 1, 150, b);


                    //lock (AgvFunctionClass.commandList)
                    {
                        AgvCommandClass acc = new AgvCommandClass();
                        acc.AgvNum = chargeState9.AgvNum;
                        acc.CurrentStation = chargeState9.CurrentStation;
                        acc.CommandName = "启动";
                        acc.CurrentTime = DateTime.Now.ToString();
                        AgvFunctionClass.commandList.Add(acc);
                    }
                }
              
               
                //lock (MainForm.lockCommand)
                //{
                //    MainForm.mainForm.comRec.AppendData(chargeState9.AgvNum, "启动");

                //}
            }
            catch
            { }
           
        }


        public void StopAgv()
        {
            try
            {
                if (isConnectSure)
                {
                    byte[] b = { 0x01 };


                    S7_9.WriteBytes(DataType.DataBlock, 1, 152, b);

                    //lock (AgvFunctionClass.commandList)
                    {
                        AgvCommandClass acc = new AgvCommandClass();
                        acc.AgvNum = chargeState9.AgvNum;
                        acc.CurrentStation = chargeState9.CurrentStation;
                        acc.CommandName = "停止";
                        acc.CurrentTime = DateTime.Now.ToString();
                        AgvFunctionClass.commandList.Add(acc);
                    }
                }
              

                //lock (MainForm.lockCommand)
                //{
                //    MainForm.mainForm.comRec.AppendData(chargeState9.AgvNum, "启动");

                //}
            }
            catch
            { }

        }



        public void DiaoAgv()
        {
            try
            {
                if (isConnectSure)
                {
                    S7_9.WriteBit(DataType.DataBlock, 1, 9, 0, 1);
                }
             

               
                //lock (MainForm.lockCommand)
                //{
                //    MainForm.mainForm.comRec.AppendData(chargeState9.AgvNum, "启动");

                //}
            }
            catch
            { }

        }

        public void UnLockAgv()
        {
            try
            {
                if (isConnectSure)
                {
                    byte[] b = { 0x00 };
                    S7_9.WriteBytes(DataType.DataBlock, 1, 154, b);
                    //lock (MainForm.lockCommand)
                    //{
                    //    MainForm.mainForm.comRec.AppendData(chargeState9.AgvNum, "解锁");

                    //}
                    //lock (AgvFunctionClass.commandList)
                    {
                        AgvCommandClass acc = new AgvCommandClass();
                        acc.AgvNum = chargeState9.AgvNum;
                        acc.CurrentStation = chargeState9.CurrentStation;
                        acc.CommandName = "解锁";
                        acc.CurrentTime = DateTime.Now.ToString();
                        AgvFunctionClass.commandList.Add(acc);
                    }
                }
              
            }
            catch
            { }
             
           
        }


      
    }


    public class PlcManagerJiGouClass
    {
        public Plc s7_jigou;
        public ChargeParaJiGouClass chargeState9 = new ChargeParaJiGouClass();
        public int jigouAgv = 0;

        public int jigouControl = 0;


        public int count9 = 0;
        public bool b9 = true;

        public Ping pingAgv = new Ping();
        public bool isConnectSure = false;

        string ip = "";
        public PlcManagerJiGouClass(string ip1)//
        {
            ip = ip1;


            s7_jigou = new Plc(CpuType.S71200, ip, 0, 1);
           
            PlcThread();

        }

        public void WriteV(bool b)
        {
            try
            { 
                if(isConnectSure)
                s7_jigou.WriteBit(DataType.DataBlock,2,0,1,b);
            }
            catch
            { }
            
        }
    
        public void PlcThread()
        {
            Thread th1 = new Thread(new ThreadStart(ChargeThreadMethod9));
            th1.IsBackground = true;
            th1.Start();
        }


        private void ChargeThreadMethod9()
        {
            while (true)
            {

                try
                {
                    if (pingAgv.Send(ip).Status == IPStatus.Success)
                    {
                      

                        if (isConnectSure)
                        {
                         
                            try
                            {
                                chargeState9.AgvAllow = (bool)s7_jigou.Read("DB2.DBX0.0");
                                chargeState9.AgvLeave = (bool)s7_jigou.Read("DB2.DBX0.1");
                                chargeState9.PlcAllow = (bool)s7_jigou.Read("DB2.DBX0.2");
                            }
                            catch
                            { }

                        }
                        else
                        {
                            try
                            {
                                s7_jigou.Open();
                                isConnectSure = true;
                            }
                            catch
                            { isConnectSure = false; }

                        }
                    }
                    else
                    {
                      


                        try
                        {
                            s7_jigou.Close();
                        }
                        catch
                        { }


                        try
                        {
                            isConnectSure = false;
                            s7_jigou = new Plc(CpuType.S71200, ip, 0, 1);

                        }
                        catch
                        { }


                    }


                }
                catch
                {
                   
                }

                Thread.Sleep(500);
            }



          
        }
    }
  
}

